package com.fancy.learncenter.common;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Hyy on 2017/3/14.
 */

public class LoginUserSharePrefernce {

    private final static String PREFERENCE_NAME = "fancy_flc_user";

    private static String customerId_key = "customerId";
    private static String mobile_key = "mobile";
    private static String headerImageUrl_key = "headerImageUrl";
    private static String password_key = "password";
    private static String update_key = "update";
    private static String name_key = "name";
    private static String fid_key = "fid";
    private static String token_key = "token";
    private static String isLogin_key = "isLogin";


    public static  void clearData() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear().commit();
    }

    public static boolean putString(String key, String value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    /**
     * 保存customerId
     *
     * @param value
     * @return
     */
    public static boolean putCustomId(long value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(customerId_key, value);
        return editor.commit();
    }

    public static long getCustomId() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getLong(customerId_key, 0);
    }


    public static boolean putMobile(String value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(mobile_key, value);
        return editor.commit();
    }

    public static String getMobile() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(mobile_key, "");
    }

    public static boolean putPassword(String value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(password_key, value);
        return editor.commit();
    }

    public static String getPassword() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(password_key, "");
    }



    public static boolean putName(String value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(name_key, value);
        return editor.commit();
    }

    public static String getUrlConfig() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        String str = settings.getString("UrlConfig", "");
        return str;
    }
    /**
     * 获取非凡学习配置信息
     * @return
     */
    public static void setUrlConfig(String urlConfig) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("UrlConfig", urlConfig);
        editor.commit();
    }

    public static String getName() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(name_key, "");
    }

    public static boolean putFid(String value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(fid_key, value);
        return editor.commit();
    }

    public static String getFid() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(fid_key, "");
    }

    public static boolean putToken(String value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(token_key, value);
        return editor.commit();
    }

    public static String getToken() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(token_key, "");
    }


    /**
     * 保存头像地址
     *
     * @param value
     * @return
     */
    public static boolean putHeaderPath(String value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(headerImageUrl_key, value);
        return editor.commit();
    }

    /**
     * 获取头像地址
     *
     * @return
     */
    public static String getHeaderPath() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(headerImageUrl_key, "");
    }


    /**
     * 是否更新个人数据
     *
     * @param value true 更新数据
     * @return
     */
    public static boolean update(boolean value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(update_key, value);
        return editor.commit();
    }

    public static boolean isUpdate() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);

        if (settings.getBoolean(update_key, false)) {
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean(update_key, false);
            editor.commit();
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否登录
     *
     * @param value true
     * @return
     */
    public static boolean login(boolean value) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(isLogin_key, value);
        return editor.commit();
    }

    /**
     * 是否登录
     *
     * @return true 已经登录   false 无法登录
     */
    public static boolean isLogin() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);

        if (settings.getBoolean(isLogin_key, false)) {
            return true;
        } else {
            return false;
        }
    }


    public static void setTime(long time) {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong("systemtime", time);
        editor.commit();
    }

    public static long getTime() {
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getLong("systemtime", 0);
    }

    /**
     *答对正确的题目数
     * @param rightAnswer
     */
    public static void setCartoonRightAnswer(int rightAnswer){

        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("do_it_again_right", rightAnswer);
        editor.commit();
    }

    public static int getCartoonRightAnswer(){

        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getInt("do_it_again_right",0);
    }

    /**
     * 保存答题时间
     * @param time
     * @param homePackageId
     */
    public static void setCartoonQuestionTime(long time,String homePackageId){
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(String.valueOf(LoginUserSharePrefernce.getCustomId() +"_" + homePackageId + "_question_time" ), time);
        editor.commit();
    }

    /**
     * 获取答题时间
     * @param homePackageId
     * @return
     */
    public static long getCartoonQuestionTime(String homePackageId){
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getLong(String.valueOf(LoginUserSharePrefernce.getCustomId() +"_" + homePackageId + "_question_time"),0);
    }

    /**
     * 记录答题开始时间
     * @param time
     * @param homePackageId
     */
    public static void setRecordStartTime(long time,String homePackageId){
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(String.valueOf(LoginUserSharePrefernce.getCustomId() +"_" + homePackageId + "_start_time" ), time);
        editor.commit();
    }

    /**
     * 获取答题开始时间
     * @param homePackageId
     * @return
     */
    public static long getRecordStartTime(String homePackageId){
        SharedPreferences settings = MyApplication.context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getLong(String.valueOf(LoginUserSharePrefernce.getCustomId() +"_" + homePackageId + "_start_time"),0);
    }


}
